package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.Result;
import com.luo.model.ResultObj;
import com.luo.service.SummaryService;
import com.luo.util.CountDown;
import com.luo.util.TokenId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/3/23 11:21
 */
@WebServlet("/member/order")
public class SubmitOrderController extends HttpServlet {

	SummaryService summaryService = new SummaryService();

	@Override
	protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
		String goods = req.getParameter("goods");
		String addressId = req.getParameter("addressId");
		Object userId = TokenId.getUserId(req, resp);
		if (userId != null && userId instanceof String) {
			String info = null;
			ResultObj result = new ResultObj();
			try {
				info = summaryService.SubmitOrderController(addressId, goods, (String) userId);
			} catch (Exception e) {
				e.printStackTrace();
				result.setCode("500");
				result.setMsg("查询失败" + e.getMessage());
				String jsonString = JSON.toJSONString(result);
				resp.getWriter().write(jsonString);
				resp.getWriter().close();
				return;
			}
			if ("-1".equals(info) || "-2".equals(info) || info == null) {
				result.setCode("500");
				result.setMsg("查询失败");
			} else {
				result.setCode("200");
				result.setMsg("查询成功");
				result.setResult(info);
			}
			String jsonString = JSON.toJSONString(result);
			resp.getWriter().write(jsonString);
			resp.getWriter().close();
		}
	}
}
